﻿using System;

using DevExpress.Xpo;

using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;
using System.ComponentModel;
using DevExpress.ExpressApp;
using DevExpress.Data.Filtering;

namespace SpeedSCM.Module
{
    [NavigationItem("库存管理")]
    [ImageName("BO_Sale_Item")]
    public class 同价调拨单 : 单据
    {
        public 同价调拨单(Session session)
            : base(session)
        {

        }
        protected override SeedType SeedType
        {
            get { return SeedType.同价调拨单; }
        }

        private 仓库 _发货仓库;
        [Custom("Caption", "发货仓库")]
        [RuleRequiredField(null, DefaultContexts.Save, "必须选择发货仓库")]
        public 仓库 发货仓库
        {
            get { return _发货仓库; }
            set { SetPropertyValue<仓库>("发货仓库", ref _发货仓库, value); }
        }

        private 仓库 _收货仓库;
        [Custom("Caption", "收货仓库")]
        [RuleRequiredField(null, DefaultContexts.Save, "必须选择收货仓库")]
        public 仓库 收货仓库
        {
            get { return _收货仓库; }
            set { SetPropertyValue<仓库>("收货仓库", ref _收货仓库, value); }
        }

        [Association("同价调拨单-同价调拨单明细")]
        public XPCollection<同价调拨单明细> 同价调拨单明细
        {
            get { return GetCollection<同价调拨单明细>("同价调拨单明细"); }
        }
        public override bool Excute(IObjectSpace objectSpace)
        {
            foreach (同价调拨单明细 item in 同价调拨单明细)
            {
                //发货仓库
                库存 s1 = objectSpace.FindObject<库存>(CriteriaOperator.Parse("商品名称=? and 仓库名称=?", item.商品名称, 发货仓库));
                //收货仓库
                库存 s2 = objectSpace.FindObject<库存>(CriteriaOperator.Parse("商品名称=? and 仓库名称=?", item.商品名称, 收货仓库));

                if (s1 != null)
                {
                    s1.库存数量 -= item.调拨数量;
                    s1.Save();
                    if (s2 == null)
                    {
                        s2 = objectSpace.CreateObject<库存>();
                        s2.商品名称 = item.商品名称;
                        s2.仓库名称 = 收货仓库;
                        s2.库存数量 = item.调拨数量;
                    }
                    else
                    {
                        s2.库存数量 += item.调拨数量;
                    }
                    s2.Save();
                }
            }
            objectSpace.CommitChanges();
            return base.Excute(objectSpace);
        }
    }

    [ImageName("BO_Sale_Item")]
    [RuleCombinationOfPropertiesIsUnique(null, DefaultContexts.Save, "同价调拨单,商品名称", CustomMessageTemplate = "同价调拨单中已经存在该商品")]
    public class 同价调拨单明细 : BaseObject
    {
        public 同价调拨单明细(Session session)
            : base(session)
        {

        }
        private 同价调拨单 _同价调拨单;
        [Custom("Caption", "同价调拨单")]
        [Association("同价调拨单-同价调拨单明细")]
        [Custom("AllowEdit", "False")]
        public 同价调拨单 同价调拨单
        {
            get { return _同价调拨单; }
            set { SetPropertyValue<同价调拨单>("同价调拨单", ref _同价调拨单, value); }
        }
        private 商品 _商品名称;
        [Custom("Caption", "商品名称")]
        [RuleRequiredField(null, DefaultContexts.Save, "必须选择商品")]
        public 商品 商品名称
        {
            get { return _商品名称; }
            set { SetPropertyValue<商品>("商品名称", ref _商品名称, value); }
        }
        private decimal _调拨数量;
        [Custom("Caption", "调拨数量")]
        [Custom("EditMask", "n2")]
        [Custom("DisplayFormat", "{0:n2}")]
        [RuleRequiredField]
        public decimal 调拨数量
        {
            get { return _调拨数量; }
            set { SetPropertyValue<decimal>("调拨数量", ref _调拨数量, value); }
        }
        private string _备注;
        [Custom("Caption", "备注")]
        [Size(500)]
        public string 备注
        {
            get { return _备注; }
            set { SetPropertyValue<string>("备注", ref _备注, value); }
        }

    }
}
